
# write by hasaki
# first edit on 2018/12/12
# last change on 2018/12/13
# 25档疯狂挂单
from collections import defaultdict

class ForDepth:
    def __init__(self):
        self.depth=None                             # 对冲交易所深度信息
        self.cash=None                              # 持有现金
        self.pos=None                               # 持有仓位

        self.other_ask1=None                        # 目标交易所第一档深度
        self.other_bid1=None                        # 目标交易所第一档深度

        self.max_volume=None                        # 用户设置某个档位区间的最大成交量

        self.orderDict=defaultdict(list)            # 输出结果
        self.orderList=[]                           # 总输出列表，档次排列

        self.other_ask_part_1=[]                    # 卖单第一区间档位
        self.other_bid_part_1=[]                    # 买单第一区间档位
        self.other_ask_part_2=[]                    # 卖单第二区间档位
        self.other_bid_part_2=[]                    # 买单第二区间档位
        self.other_ask_part_3=[]                    # 卖单第三区间档位
        self.other_bid_part_3=[]                    # 买单第三区间档位

        self.mid_move=None                          # 中值偏移阈值
        self.average_move=None                      # 平均值偏移阈值
        self.time_limit=None                        # 时间段更新阈值

        self.update_result=[]                       # 需要更新的档位[1,2,3]分别对应第一档位区间，第二档位区间，第三档位区间

        self.max_pos1=None                          # 第一档区挂挡总volume
        self.max_pos2=None                          # 第二档区挂单总volume
        self.max_pos3=None                          # 第三档区挂单总volume

        self.divie=[1/16,3/32,1/8,1/16,5/32,3/16,5/32,5/32]
    # ----------------------------------------------------------------------------------------------
    # 获取数据
    def dataIn(self,depth):
        '''目标交易所深度，值要买一卖一即可，为了拉开第一档之间的价格区间用于刷量，这里用对冲交易所的第三档价格'''
        self.depth=depth

        for i in range(3):
            self.depth['asks'].pop(0)
            self.depth['bids'].pop(0)

        self.other_ask1=depth['asks'][0]      # 目标交易所买3深度数据 [price,volume]
        self.other_bid1=depth['bids'][0]      # 目标交易所卖3深度数据
    
    def maxPos(self,max_pos1,max_pos2,max_pos3):
        '''三个档位区间的volume总值,type=float'''
        self.max_pos1=max_pos1
        self.max_pos2=max_pos2
        self.max_pos3=max_pos3

    # ----------------------------------------------------------------------------------------------
    # 数据处理
    def divieOtherList(self):
        '''把深度分成3档'''
        counter=0
        for i,j in zip(self.depth['asks'],self.depth['bids']):
            if counter<8:
                self.other_ask_part_1.append(i)
                self.other_bid_part_1.append(j)
            elif counter<16:
                self.other_ask_part_2.append(i)
                self.other_bid_part_2.append(j)
            elif counter<24:
                self.other_ask_part_3.append(i)
                self.other_bid_part_3.append(j)
            counter+=1
    
    # ----------------------------------------------------------------------------------------------
    def makeAskVolume(self):
        '''根据档位区间生成档位挂单的volume'''
        self.divie.reverse()

        for ask_1,a_vol_1 in zip(self.other_ask_part_1,self.divie):
            ask_1[1]=round(self.max_pos1*a_vol_1,4)
            ask_1[0]=round(ask_1[0],4)
        
        for ask_2,a_vol_2 in zip(self.other_ask_part_2,self.divie):
            ask_2[1]=round(self.max_pos2*a_vol_2,4)
            ask_2[0]=round(ask_2[0],4)
        
        for ask_3,a_vol_3 in zip(self.other_ask_part_3,self.divie):
            ask_3[1]=round(self.max_pos3*a_vol_3,4)
            ask_3[0]=round(ask_3[0],4)

    def makeBidVolume(self):
        '''根据档位区间生成档位挂单的volume'''
        self.divie.reverse()
        for bid_1,b_vol_1 in zip(self.other_bid_part_1,self.divie):
            bid_1[1]=round(self.max_pos1*b_vol_1,4)
            bid_1[0]=round(bid_1[0],4)
        
        for bid_2,b_vol_2 in zip(self.other_bid_part_2,self.divie):
            bid_2[1]=round(self.max_pos2*b_vol_2,4)
            bid_2[0]=round(bid_2[0],4)
        
        for bid_3,b_vol_3 in zip(self.other_bid_part_3,self.divie):
            bid_3[1]=round(self.max_pos3*b_vol_3,4)
            bid_3[0]=round(bid_3[0],4)

    # ----------------------------------------------------------------------------------------------
    # 运行
    def run(self,mode,depth_num):
        '''mode 分ask模式和bid模式，适用多账户管理,mode为'asks'和'bids' '''
        # 先清空过往记录

        if mode=='asks':
            self.other_ask_part_1.clear()
            self.other_ask_part_2.clear()
            self.other_ask_part_3.clear()
            self.divieOtherList()
            self.makeAskVolume()
            # 分档区间保存
            if depth_num==1:
                return self.other_ask_part_1
            elif depth_num==2:
                return self.other_ask_part_2
            elif depth_num==3:
                return self.other_ask_part_3

        elif mode=='bids':
            self.other_bid_part_1.clear()
            self.other_bid_part_2.clear()
            self.other_bid_part_3.clear()
            self.divieOtherList()
            self.makeBidVolume()
            # 分档区间保存
            if depth_num==1:
                return self.other_bid_part_1
            elif depth_num==2:
                return self.other_bid_part_2
            elif depth_num==3:
                return self.other_bid_part_3
